package com.baofu.paycore.manager.storage.impl;

import com.baofu.paycore.common.exception.ManagerErrorCode;
import com.baofu.paycore.common.utils.Validate;
import com.baofu.paycore.dal.mapper.SubTransferMapper;
import com.baofu.paycore.dal.model.SubTransferDO;
import com.baofu.paycore.manager.storage.Storage;
import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import com.system.commons.exception.BizServiceException;
import com.system.commons.utils.ParamValidate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * description:转账资金单操作
 *
 * <p>
 *      1.新增转账资金单
 *      2.跟新转账资金单
 *      3.根据id获取转账资金单
 *      4.根据payNo获取转账资金单
 *      5.分页获取转账资金单
 *      6.获取转账资金单数量
 * </p>
 * User: liu ting  Date: 2016/5/19 ProjectName: paycore Version: 1.0
 */
@Slf4j
@Repository
public class SubTransferManager implements Storage<SubTransferDO> {

    /**
     * 转账资金单操作mapper
     */
    @Autowired
    private SubTransferMapper subTransferMapper;


    /**
     * 1.新增转账资金单
     *
     * @param subTransferDO 转账资金单
     */
    @Override
    public int addOrder(SubTransferDO subTransferDO){
        int count = subTransferMapper.insert(subTransferDO);
        Validate.checkUpdate(count);
        return count;
    }


    /**
     * 2.跟新转账资金单
     *
     * @param subTransferDO 转账资金单
     */
    @Override
    public int modifyOrder(SubTransferDO subTransferDO) {
        if (StringUtils.isNotEmpty(subTransferDO.getErrorReason()) &&
                subTransferDO.getErrorReason().length() > 250){
            subTransferDO.setErrorReason(subTransferDO.getErrorReason().substring(0, 250));
        }
        int count = subTransferMapper.update(subTransferDO);
        Validate.checkUpdate(count, PayCoreErrorCode.UPDATE_DATA_NOT_ONE.getErrorDesc());
        return count;
    }

    /**
     * 3.根据id获取转账资金单
     *
     * @param id 主键id
     * @return 转账资金单
     */
    public SubTransferDO getById(Long id){

        SubTransferDO transferDO = subTransferMapper.getById(id);
        if(transferDO == null){
            log.error("根据id获取转账资金单异常，查询内容不存在");
            throw new BizServiceException(PayCoreErrorCode.EMPTY_QUERY_RESULT);
        }

        return transferDO;
    }

    /**
     * 4.根据payNo获取转账资金单
     *
     * @param payNo 支付号
     * @return 转账资金单
     */
    public List<SubTransferDO> selectByPayNo(String payNo){

        List<SubTransferDO> subTransferDO = subTransferMapper.selectByPayNo(payNo);
        if(CollectionUtils.isEmpty(subTransferDO)){
            log.error("根据payNo获取转账资金单异常，查询内容不存在");
            throw new BizServiceException(PayCoreErrorCode.EMPTY_QUERY_RESULT);
        }
        return subTransferDO;
    }

    /**
     * 5.分页获取转账资金单
     *
     * @param subTransferDO 转账资金单
     * @param beginPage 开始页
     * @param pageSize 每页数量
     * @return 转账资金单
     */
    public List<SubTransferDO> selectSubTransferPage(SubTransferDO subTransferDO, int beginPage, int pageSize){

        return subTransferMapper.selectSubTransferByPage(subTransferDO, beginPage, pageSize);
    }

    /**
     * 6.获取转账资金单数量
     *
     * @param subTransferDO 转账资金单
     * @return 数量
     */
    public int selectSubTransferCount(SubTransferDO subTransferDO){

        int count = subTransferMapper.selectSubTransferCount(subTransferDO);
        ParamValidate.validateQueryCount(count,PayCoreErrorCode.EMPTY_QUERY_RESULT.getErrorDesc());

        return count;
    }

    /**
     * 废弃订单
     *
     * @param subTransferDO 订单信息
     */
    @Override
    public int deleteOrder(SubTransferDO subTransferDO) {
        return 0;
    }
}
